package
{
	import Components.MovementComponent;
	
	import as3isolib.display.primitive.IsoBox;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.IEventDispatcher;
	import flash.geom.Point;

	public class HomeBase implements IGameObject, IAttackable
	{
		public var health:Number = 100;
		public var gasGathered:int = 0;
		public var mineralGathered:int = 0;
		public var displayContainer:DisplayObjectContainer;
		public var movementComponent:MovementComponent = new MovementComponent;
		public var display:Sprite = new Sprite();
		public var isoDisplay:IsoBox = new IsoBox;
		public var gasNode:ResourceNode;
		public var enemyHomeBase:HomeBase;
		public var gasGatherers:Vector.<ResourceGatherer> = new Vector.<ResourceGatherer>;
		public var attackers:Vector.<Attacker> = new Vector.<Attacker>;
		
		public function HomeBase()
		{
			var bg:Sprite = VTUtil.getRectOutline(20,20,0x0000ff,2);
			bg.x = -bg.width/2;
			bg.y = -bg.height/2;
			display.addChild(bg);
			isoDisplay.setSize(10,10,10);
		}
		
		public function update(delta:int):void{
			var i:int, l:int;
			movementComponent.update(delta);
			
			for(i=0, l=gasGatherers.length; i<l; i++){
				gasGatherers[i].update(delta);
			}
			
			for(i=0, l=attackers.length; i<l; i++){
				attackers[i].update(delta);
			}			
			
//			display.x = movementComponent.location.x * rts1.RATIO.x;
//			display.y = movementComponent.location.y * rts1.RATIO.y;
			isoDisplay.moveTo(movementComponent.location.x * rts1.RATIO.x, movementComponent.location.y * rts1.RATIO.x, 0);
			
		}
	
		
		public function createAttacker():void{
			var attacker:Attacker = new Attacker();
			attacker.attackRange = 1.5;
			attacker.homeBaseToAttack = enemyHomeBase;
			attacker.movementComponent.location = this.movementComponent.location.clone();
			attackers.push(attacker);
			attacker.displayContainer = this.displayContainer;
			attacker.homeBaseDestructCallback = onAttackerDestruction;
			this.gasGathered -= 10;
			
			displayContainer.addChild(attacker.display);			
		}
		
		private function onAttackerDestruction(attacker:Attacker):void{
			attacker.display.visible = false;
			if(attacker.display.parent){
				attacker.display.parent.removeChild(attacker.display);
			}
			attackers.splice(attackers.indexOf(attacker),1);
		}
		
		public function createGasGatherer():void{
			var gasGatherer:ResourceGatherer = new ResourceGatherer();
			gasGatherer.resourceType = rts1.RESOURCE_TYPE_GAS;
			gasGatherer.resourceNode = gasNode;
			gasGatherer.homeBase = this;
			gasGatherer.movementComponent.location = this.movementComponent.location.clone();
			gasGatherers.push(gasGatherer);
			
			this.gasGathered -= 30;
			
			displayContainer.addChild(gasGatherer.display);
		}
		
		/**
		 * IAttackable
		 * **/
		public function getLocation():Point {
			return movementComponent.location;
		}
		public function inflictDamage(amount:Number):void{
			health -= amount;
		}
		public function getHealth():Number{
			return health;
		}
	}
}